﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using GrewalCRM.Entities.Common;
using GrewalCRM.Entities;
using GrewalCRM.BLL;
using AjaxControlToolkit;

public partial class CallSatusUC : System.Web.UI.UserControl
{
  #region Event Handalrs
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      Bind();
      ddlStatus.SelectedValue = ((int)CallStatus.New).ToString();
    }
  }
  public void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
  {
    if (ddlStatus.SelectedValue == "0")
      return;

    CallStatus seletcedStatus = (CallStatus)Enum.Parse(typeof(CallStatus), ddlStatus.SelectedItem.Text);

    switch (seletcedStatus)
    {      
      case CallStatus.Commit:
        mvwStatus.SetActiveView(vwStatusCommit);
        break;
      case CallStatus.Assigned:
        mvwStatus.SetActiveView(vwStatusAssigned);
        break;
      case CallStatus.Completed:
        mvwStatus.SetActiveView(vwStatusCompleted);
        break;   
    }

    if (sender != null)
    {
      ((getContainerCell(sender as DropDownList).FindControl("popupEditCall")) as ModalPopupExtender).Show();
    }
    

  } 

  Control getContainerCell(Control ctl)
  {
    if (ctl is DataControlFieldCell)
      return ctl;
    else
    {
      ctl = getContainerCell(ctl.Parent);
      return ctl;
    }
  }
  #endregion

  #region Public Properties
  public DateTime? ScheduledDate
  {
    get { return UCdpScheduledDate.Date; }
    set { UCdpScheduledDate.Date = value; }
  }
  public int InstallerID
  {
    get { return int.Parse(ddlInstaller.SelectedItem.Value); }
    set {ddlInstaller.SelectedValue = value.ToString();}
  }
  public int CallStatusID
  {
    get { return int.Parse(ddlStatus.SelectedItem.Value); }
    set 
    {
      if (value == 0)
        return;

      CallStatus callStatus = (CallStatus)value;
      switch(callStatus)
      {
        case CallStatus.New:            
          rePopulateStatusAsPerRule(CallStatus.New, CallStatus.Cancel);
          break;
        case CallStatus.Pending:            
          rePopulateStatusAsPerRule(CallStatus.Pending, CallStatus.Cancel);           
          break;
        case CallStatus.Commit:
          rePopulateStatusAsPerRule(CallStatus.Commit, CallStatus.Assigned);       
          break;
        case CallStatus.Cancel:
          rePopulateStatusAsPerRule(CallStatus.Cancel, CallStatus.Cancel);
          break;
        case CallStatus.Assigned:
          rePopulateStatusAsPerRule(CallStatus.Cancel, CallStatus.Completed);               
          break;
        case CallStatus.Completed:
          rePopulateStatusAsPerRule(CallStatus.Completed, CallStatus.Invoiced);
          break;
        case CallStatus.Invoiced:
          rePopulateStatusAsPerRule(CallStatus.Invoiced, CallStatus.Closed);
          break;
        case CallStatus.Closed:
          rePopulateStatusAsPerRule(CallStatus.Closed, CallStatus.Closed);
          break;
      }
         
      ddlStatus.SelectedValue = value.ToString(); 
    }
  }
  public decimal InstallerCommisionCR
  {
    get
    {
      if (string.IsNullOrEmpty(txtInstallerCommisionCR.Text.Trim()))
        return 0;
      else
        return Convert.ToDecimal(txtInstallerCommisionCR.Text);
    }
    set { txtInstallerCommisionCR.Text = value.ToString(); }
  }
  public decimal InstallerMaterialCR
  {
    get
    {
      if (string.IsNullOrEmpty(txtInstallerMaterialCR.Text.Trim()))
        return 0;
      else
        return Convert.ToDecimal(txtInstallerMaterialCR.Text);
    }
    set { txtInstallerMaterialCR.Text = value.ToString(); }
  }
  public decimal InstallerAcDR
  {
    get
    {
      if (string.IsNullOrEmpty(txtInstallerAcDR.Text.Trim()))
        return 0;
      else
        return Convert.ToDecimal(txtInstallerAcDR.Text);
    }
    set { txtInstallerAcDR.Text = value.ToString(); }
  }
  public decimal DealerAcDR
  {
    get
    {
      if (string.IsNullOrEmpty(txtDealerAcDR.Text.Trim()))
        return 0;
      else
        return Convert.ToDecimal(txtDealerAcDR.Text);
    }
    set { txtDealerAcDR.Text = value.ToString(); }
  }
  public decimal DealerAcCR
  {
    get
    {
      if (string.IsNullOrEmpty(txtDealerAcCR.Text.Trim()))
        return 0;
      else
        return Convert.ToDecimal(txtDealerAcCR.Text);
    }
    set { txtDealerAcCR.Text = value.ToString(); }
  }
  public DateTime? CompletedDate
  {
    get { return UCdpCompletionDate.Date; }
    set { UCdpCompletionDate.Date = value; }
  }
  public bool? IsTempClosed
  {
    get { return chkIsTempClosed.Checked; }
    set 
    {
      if (value.HasValue)
        chkIsTempClosed.Checked = value.Value;
      else
        chkIsTempClosed.Checked = false;
    }
  }
  public bool Enabled
  {
    get { return pnlCallStatus.Enabled; }
    set { pnlCallStatus.Enabled = value; }
  }
  public string ValidationGroup
  {
    get { return reqDdlInstaller.ValidationGroup; }
    set
    {
      foreach (Control ctl in pnlCallStatus.Controls)
      {
        if (ctl is BaseValidator)
          (ctl as BaseValidator).ValidationGroup = value;          
      }
    }
  }  
  #endregion

  #region Private Method/Property
  private ImasterCRM MasterCRM
  {
    get { return this.Page.Master as ImasterCRM; }
  }
  public void PopulateStatus()
  {      
    ddlStatus.DataSource = MasterCRM.CallStatusCache;
    ddlStatus.DataTextField = "StatusName";
    ddlStatus.DataValueField = "ID";
    ddlStatus.DataBind();
    CommonUtil.AddDefaultItemToDDL(ddlStatus,"Call Status");
  }
  public void PopulateInatallers()
  {      
    ddlInstaller.DataSource = MasterCRM.InstallersCache;
    ddlInstaller.DataTextField = "InstallerName";
    ddlInstaller.DataValueField = "ID";
    ddlInstaller.DataBind();
    CommonUtil.AddDefaultItemToDDL(ddlInstaller,"Installer");
  }
  public void Bind()
  {
    PopulateStatus();
    PopulateInatallers();
  }
  private void rePopulateStatusAsPerRule(CallStatus from, CallStatus to)
  {
    int startingStatus = (int)from;
    int endingStatus = (int)to;
    int length = ddlStatus.Items.Count;

    for (int i = 1; i <= length; i++)
    {
      if (i < startingStatus || i > endingStatus)
        ddlStatus.Items.Remove(ddlStatus.Items.FindByValue(i.ToString()));
    }
  }
  #endregion        
}